package com.yuanjiu.service.w1919;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yuanjiu.dao.yuanjiu.DataMergeDao;
import com.yuanjiu.pojo.yuanjiu.CategoryIdName;
import com.yuanjiu.pojo.yuanjiu.CategoryObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

@Service("w1919BrandAquire")//标注业务层组件
public class W19Brand {

	@Autowired
	@Qualifier("w1919BrandOutput")
	private BrandOutput brandOutput;
	
   public void process(String... param){

//		"https://www.1919.cn/search.html?cat=337063315819859968_%E7%99%BD%E9%85%92",
//		"https://www.1919.cn/search.html?cat=337063315824054272_%E8%91%A1%E8%90%84%E9%85%92",
//		"https://www.1919.cn/search.html?cat=337063315769528320_%E6%B4%8B%E9%85%92",
//		"https://www.1919.cn/search.html?cat=337063315819859969_%E5%95%A4%E9%85%92",
//		"https://www.1919.cn/search.html?cat=337063315824054273_%E9%BB%84%E9%85%92",
//		"https://www.1919.cn/search.html?cat=337063315824054274_%E6%9E%9C%E9%85%92"
		Spider.create(new BrandInput()).addPipeline(brandOutput).addUrl(param).run();

	}
}

class BrandInput implements PageProcessor{
	
	private Site site = Site.me().setTimeOut(5000).setRetryTimes(3).setRetrySleepTime(2);

	@Override
	public void process(Page page) {

		String name = null;

		Set<String> tmpCollection = new HashSet<String>();

		// 白酒
		if (page.getUrl().regex(".*/search.html(.*)?cat=337063315819859968.*").match()) {

			name = "白酒";
		}
		// 葡萄酒
		else if (page.getUrl().regex(".*/search.html(.*)?cat=337063315824054272.*").match()) {

			name = "葡萄酒";
		}
		// 洋酒
		else if (page.getUrl().regex(".*/search.html(.*)?cat=337063315769528320.*").match()) {

			name = "洋酒";
		}
		// 啤酒
		else if (page.getUrl().regex(".*/search.html(.*)?cat=337063315819859969.*").match()) {

			name = "啤酒";
		}
		// 黄酒
		else if (page.getUrl().regex(".*/search.html(.*)?cat=337063315824054273.*").match()) {

			name = "黄酒";
		}
		// 果酒
		else if (page.getUrl().regex(".*/search.html(.*)?cat=337063315824054274.*").match()) {

			name = "果酒";
		}
		
		Selectable xx = page.getHtml().xpath("//*[@id='brand']/a");

		java.util.List<Selectable> mm = xx.nodes();

		for (Selectable tmp : mm) {

			//抓取品牌名字
			String brandName=tmp.xpath("/a/@name").get().substring(3);
			
			if (!tmpCollection.contains(brandName)) {

				tmpCollection.add(brandName);
			}
		}

		page.putField(name, tmpCollection);//将抓到的品牌名字放到对应的分类结果集里
	}

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}
}

@Service("w1919BrandOutput")
class BrandOutput implements Pipeline {
	
//	@Autowired
//	@Qualifier("w1919Bean")
//	private CategoryDao categoryDao;
	@Autowired
	private DataMergeDao dataMergeDao;

	@SuppressWarnings("unchecked")
	@Override
	public void process(ResultItems resultItems, Task task) {

		Map<String, Object> result = resultItems.getAll();//将所有结果以键值对的形式存入result中

		for (Map.Entry<String, Object> tmp : result.entrySet()) {
			//遍历map中的键值对key和value
			
			//获取分类名称
			List<CategoryIdName> categoryIdNames;
			try {
				
				categoryIdNames=dataMergeDao.getCategoryName();
			
				String cname = tmp.getKey();//获取结果集的key值，即品牌类别
				Integer catid = null;
				
			    //将数据库中提取的名字与抓取的结果集的名字比较
				for(CategoryIdName categoryIdName:categoryIdNames){
					if(categoryIdName != null && StringUtils.isNotEmpty(categoryIdName.getCat()) && 
							categoryIdName.getCat().equals(cname)){
						catid=categoryIdName.getId();
					}
				}
				
				//如果没有匹配到，3对应其他类别
				if(catid==null){ 
					catid=0;
				}

				//获取类别下所有品牌
				List<String> categoryObjects=dataMergeDao.getCategoriesByParent(catid); 
	            //记录下数据库中匹配到的类别下的所有品牌名字name

				//获取品牌名字
				Set<String> cvalue = (Set<String>) tmp.getValue();
				
				cvalue.add("其他");
          
				//将刚刚处理的抓取结果得到的品牌名字跟数据库的名称匹配
				List<CategoryObject> list=new ArrayList<CategoryObject>();
				for (String valuetmp : cvalue) {
					//名称已存在则跳过
					if(categoryObjects.contains(valuetmp)){
						continue;
					}
					CategoryObject categoryObject2 = new CategoryObject();//存入新品牌名字
					categoryObject2.setParent(catid);
					categoryObject2.setName(valuetmp);
					
					// 插入新分类到数据库中
					list.add(categoryObject2);
					
				}
				if(list.size()>0){
					dataMergeDao.insertCategory(list); 
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}

	}
	

}